package com.xms.form.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.xms.datacore.service.DeleterService;
import com.xms.dependency.XmsDependencyException;
import com.xms.event.core.AuthorizationStateChangedEvent;
import com.xms.event.core.IEventPublisher;
import com.xms.form.core.SystemFormDefaults;
import com.xms.form.entity.SystemForm;
import com.xms.form.event.SystemFormDeletedEvent;
import com.xms.utils.CollectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * SystemformDeleterService
 * </p>
 *
 * @author migo
 * @since 2021/9/3
 */
@CacheConfig(cacheNames = "systemform", keyGenerator = "CacheKeyGenerator")
@Service
public class SystemFormDeleterService extends DeleterService<SystemForm> implements ISystemFormDeleterService {
    private ISystemFormDependency _systemFormDependency;
    private IEventPublisher _eventPublisher;

    @Autowired
    public SystemFormDeleterService(ISystemFormDependency systemFormDependency,
                                    IEventPublisher eventPublisher) {
        _systemFormDependency = systemFormDependency;
        _eventPublisher = eventPublisher;
    }

    @CacheEvict(allEntries = true, condition = "#result == true")
    @Transactional(rollbackFor=Exception.class)
    @Override
    public boolean deleteById(UUID id) throws SQLException, XmsDependencyException {
        SystemForm deleted = super.getById(id);
        if (deleted == null)
            return false;
        return this.delete(deleted);
    }

    @CacheEvict(allEntries = true, condition = "#result == true")
    @Transactional(rollbackFor=Exception.class)
    @Override
    public boolean delete(SystemForm deleted) throws SQLException, XmsDependencyException {
        boolean success = super.deleteById(deleted.getSystemformid());
        if (success) {
            //依赖项
            _systemFormDependency.delete(deleted.getSystemformid());
            //权限更改事件
            _eventPublisher.publish(new AuthorizationStateChangedEvent(SystemFormDefaults.MODULE_NAME, Collections.singletonList(deleted.getSystemformid()), false));
            //删除事件
            _eventPublisher.publish(new SystemFormDeletedEvent(SystemFormDefaults.MODULE_NAME, deleted));
        }
        return success;
    }

    @CacheEvict(allEntries = true, condition = "#result == true")
    @Transactional(rollbackFor=Exception.class)
    @Override
    public boolean deleteByIds(Collection<UUID> idList) throws XmsDependencyException, SQLException {
        if (CollectionUtil.isEmpty(idList)) {
            return false;
        }
        List<SystemForm> deleteds = super.listByIds(idList);
        if (CollectionUtil.notEmpty(deleteds)) {
            for (SystemForm entity : deleteds) {
                this.delete(entity);
            }
            return true;
        }
        return false;
    }

    @CacheEvict(allEntries = true, condition = "#result == true")
    @Transactional(rollbackFor=Exception.class)
    @Override
    public boolean deleteMany(Wrapper<SystemForm> queryWrapper) throws SQLException, XmsDependencyException {
        List<SystemForm> deleteds = super.list(queryWrapper);
        if (CollectionUtil.notEmpty(deleteds)) {
            for (SystemForm entity : deleteds) {
                this.delete(entity);
            }
            return true;
        }
        return false;
    }
}
